<?php

namespace App\Filament\Resources;

use App\Filament\Resources\MessageResource\Pages;
use App\Models\Message;
use Filament\Forms\Components\Radio;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Actions\CreateAction;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\EditAction;

class MessageResource extends Resource
{
    protected static ?string $model = Message::class;

    protected static ?string $navigationIcon = 'heroicon-o-chat-bubble-bottom-center-text';

    protected static ?int $navigationSort = 2;

    // 导航项分组翻译
    public static function getNavigationGroup(): ?string
    {
        return __('menu.website_group.name');
    }

    // 菜单标题导航翻译
    public static function getNavigationLabel(): string
    {
        return __('menu.website_group.message');
    }

    // 面包屑导航翻译
    public static function getModelLabel(): string
    {
        return __('menu.website_group.message');
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Textarea::make('content')
                    ->label(__('message.Content'))
                    ->rows(4)
                    ->disabled(),
                Textarea::make('reply_content')
                    ->label(__('message.Reply_content'))
                    ->rows(5),
                Radio::make('status')
                    ->label(__('category.Status'))
                    ->options([
                        '0' => '隐藏',
                        '1' => '显示',
                    ])
                    ->inline()
                    ->inlineLabel(false)
                    ->default('1')
                    ->required(),
            ])
            ->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')->label(__('message.Name')),
                TextColumn::make('email')
                    ->label(__('message.Email'))
                    ->badge(),
                TextColumn::make('content')
                    ->label(__('message.Content'))
                    ->limit(50),
                TextColumn::make('created_at')
                    ->label(__('message.Created_at'))
                    ->dateTime('Y年m月d日 H:i:s'),
                TextColumn::make('status')
                    ->label(__('category.Status'))
                    ->badge()
                    ->color(fn(string $state): string => match ($state) {
                        '0' => 'gray',
                        '1' => 'success',
                    })
                    ->formatStateUsing(fn(string $state): string => match ($state) {
                        '0' => '隐藏',
                        '1' => '显示',
                    })
                    ->alignCenter(),
                TextColumn::make('ip')->label(__('message.Ip'))->badge()->alignCenter(),
            ])
            ->filters([
                //
            ])
            ->actions([
                EditAction::make()->label('回复')->icon('heroicon-o-chat-bubble-left'),
                DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('id', 'desc');
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListMessages::route('/')
        ];
    }
}
